package com.jielin.handling.controller.camera;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jielin.handling.Annotation.MyPower;
import com.jielin.handling.bean.AppResult;
import com.jielin.handling.bean.user.User;
import com.jielin.handling.bean.user.UserSearch;
import com.jielin.handling.entity.camera.Camera;
import com.jielin.handling.page.DataGrid;
import com.jielin.handling.service.camera.CameraService;
import com.jielin.handling.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 类名称：CameraController
 * 创建人：zk
 * 创建时间：
 * 类描述：摄像头信息增删改查
 */
@RestController
@RequestMapping("/camera")
@Slf4j
@CrossOrigin
public class CameraController {

    @Autowired
    private CameraService cameraService;

    //查询摄像头信息
    @MyPower(name = "新增摄像头信息", url = "/selectCamera", isPublic = true)
    @RequestMapping(value="/selectCamera")
    public AppResult selectCamera(@RequestBody Camera camera){
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        try {
            List<Camera>cameraList =cameraService.selectCamera(camera);
            result.setCode(200);
            result.setData(data);
            result.setMsg("查询成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setData(data);
            result.setMsg("查询失败");
        }
        return result;
    }


    //新增摄像头信息
    @MyPower(name = "新增摄像头信息", url = "/addCamera", isPublic = true)
    @RequestMapping(value="/addCamera")
    public AppResult addCamera(@RequestBody JSONObject camera){
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        try {
            cameraService.addCamera(camera);
            result.setCode(200);
            result.setData(data);
            result.setMsg("新增成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(500);
            result.setData(data);
            result.setMsg("新增失败");
        }
        return result;
    }

    //修改摄像头信息
    @MyPower(name = "修改摄像头信息", url = "/updateCamera", isPublic = true)
    @RequestMapping(value="/updateCamera")
    public AppResult updateCamera(@RequestBody Camera camera){
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        int i =  cameraService.updateCamera(camera);
        if(i ==1){
            result.setCode(200);
            result.setData(data);
            result.setMsg("修改成功");
        }else{
            result.setCode(500);
            result.setData(data);
            result.setMsg("修改失败");
        }
        return result;
    }
    //查询恩施州下的所有市县
    @MyPower(name = "查询恩施州下的所有市县", url = "/selectCity", isPublic = true)
    @RequestMapping(value="/selectCity")
    public AppResult selectCity(){
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        List<Map<String, Object>>map =cameraService.selectCity();
        data.put("datagrid",map);
        result.setCode(200);
        result.setData(data);
        result.setMsg("查询成功");
        return result;
    }

    //查询市县下的派出所
    @MyPower(name = "查询市县下的派出所", url = "/selectPoliceStation", isPublic = true)
    @RequestMapping(value="/selectPoliceStation")
    public AppResult selectPoliceStation(@RequestBody JSONObject jsonObject){
        AppResult result = new AppResult();
        JSONObject data = new JSONObject();
        if(jsonObject.getString("handleUnitCode").isEmpty()){
            result.setCode(300);
            result.setData(data);
            result.setMsg("请传入单位编号");
            return result;
        }
        List<Map<String, Object>>map =cameraService.selectPoliceStation(jsonObject);
        data.put("datagrid",map);
        result.setCode(200);
        result.setData(data);
        result.setMsg("查询成功");
        return result;
    }



}
